import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.io.*;

public class TextEditor extends Form implements CommandListener{
	private FileManagerMIDlet filemanager;
	private Display display;
	private Command back, save, discard, ok, cancel, exit;
	private Form configform, nameform;
	private TextField textcontend, namefield;
	private String name, contend, url;
	private boolean newdoc, saved;
	private FileConnection fileconn;
	private Connector conn;
	private ListContend list;
	private InputStream is;
	private OutputStream os;
	public TextEditor(FileManagerMIDlet fm, Display dp, ListContend list){
		super("");		
		newdoc = true;
		saved = false;
		this.filemanager = fm;
		this.display = dp;
		this.list = list;		
		textcontend = new TextField("Nhap noi dung","", 10000, TextField.ANY);
		namefield = new TextField("Nhap ten file (khong can '.txt')","", 100, TextField.ANY);		
		append(textcontend);
		back = new Command("Tro ve", Command.BACK, 1);
		save = new Command("Luu", Command.OK, 0);
		discard = new Command("Khong", Command.OK, 1);
		ok = new Command("Dong y", Command.OK, 1);
		cancel = new Command("Tro ve", Command.BACK, 1);
		exit = new Command("Dong", Command.EXIT, 1);
		configform = new Form("Xac nhan");
		nameform = new Form("");
		nameform.addCommand(ok);
		nameform.addCommand(cancel);
		nameform.addCommand(exit);
		nameform.append(namefield);
		nameform.setCommandListener(this);
		configform.append("Ban co muon luu lai nhung thay doi ?");
		configform.addCommand(save);
		configform.addCommand(discard);
		addCommand(save);
		addCommand(back);
		addCommand(exit);
		setCommandListener(this);
	}
	public boolean checkExist(String filepath){
		try{
			fileconn = (FileConnection) conn.open(filepath + ".txt");	
			if(fileconn.exists() == true){
				return true;					
			}
			else		
				return false;			
		}
		catch(Exception e){			
			return true;
		}
	}
	public void Save(String filepath, String contend){
		try{
			fileconn = (FileConnection) conn.open(filepath + ".txt");
			fileconn.create();
			byte[] contendbyte = contend.getBytes();		
			os = conn.openOutputStream(filepath + ".txt");
			os.write(contendbyte);
			os.close();
		}		
		catch(Exception e){}		
		//filemanager.showContend(list.URL(), null);
	}
	public void commandAction(Command c, Displayable d){
		if(c == back){
			display.setCurrent(list);
		}		
		if(c == ok){
			try{
			url = list.URL();
			name = namefield.getString();	
			contend = textcontend.getString();		
			}
			catch(Exception e){filemanager.showAlert("Loi trong ten !", null);}
			if(checkExist(url+name) == false)
				filemanager.showAlert("Da kiem tra ton tai !", null);
				//Save(url + name, contend);
			//else
				//display.setCurrent(nameform);
				//filemanager.showAlert("Ten file nay da co roi !", null);											
		}
		if(c == cancel){
			display.setCurrent(this);
		}
		if(c == save){			
			display.setCurrent(nameform);
		}
		if(c == discard){
			display.setCurrent(list);
		}
		if(c == exit){
			if(newdoc == true){
				if(textcontend.size() == 0)
					display.setCurrent(list);
				else{
					display.setCurrent(configform);
				}
			}
			else{}
		}
	}	
}